package com.hnichr.ump.salon.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hnichr.ump.common.salon.entity.Applicants;
import com.hnichr.ump.common.salon.service.ApplicantsService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/salon/applicants/v1/")
@Tag(name = "ApplicantsController", description = "申请人维护接口")
public class ApplicantsController {

    @Autowired
    private ApplicantsService applicantsService;

    @GetMapping("list")
    @Operation(summary = "申请人信息列表", description = "申请人信息列表（无分页）", method = "GET")
    public Result<List<Applicants>> list(@Parameter(description="查询参数") Criteria criteria){
        List<Applicants> list = applicantsService.list(Applicants.class,criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "申请人信息列表", description = "申请人信息列表（分页）", method = "GET")
    public Result<Page<Applicants>> page(@Parameter(description="查询参数")  Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<Applicants> list = applicantsService.listPage(Applicants.class,page, criteria);
        return Result.OK(list);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取申请人信息", description = "根据ID获取申请人信息", method = "GET")
    public Result<Applicants> getById(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        Applicants applicant = applicantsService.getById(Applicants.class,id);
        return Result.OK(applicant);
    }

    @PostMapping("add")
    @Operation(summary = "新增申请人信息", description = "新增申请人信息", method = "POST")
    public Result<Applicants> add(@RequestBody Applicants applicant) {
        applicantsService.save(applicant);
        return Result.OK(applicant);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑申请人信息", description = "编辑申请人信息", method = "PUT")
    public Result<Applicants> edit(@RequestBody Applicants applicant) {
        applicantsService.update(applicant);
        return Result.OK(applicant);
    }
    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除申请人信息", description = "根据id删除申请人信息", method = "DELETE")
    public Result<Applicants> remove(@PathVariable(name = "ids", required = true) String ids) {
        applicantsService.remove(ids);
        return Result.OK();
    }

    @PostMapping("pass")
    @Operation(summary = "审核通过", description = "审核通过", method = "POST")
    public Result<?> pass(@RequestBody String ids) {
        if (StrUtil.isEmpty(ids)) {
            return Result.error("ids不能为空");
        }
        JSONObject jsonObject = JSONUtil.parseObj(ids);
        String idsStr = jsonObject.getStr("ids");
        if (StrUtil.isEmpty(idsStr)) {
            return Result.error("ids不能为空");
        }
        applicantsService.pass(idsStr);
        return Result.OK();
    }

    @PostMapping("nopass")
    @Operation(summary = "审核不通过", description = "审核不通过", method = "POST")
    public Result<?> nopass(@RequestBody String ids) {
        if (StrUtil.isEmpty(ids)) {
            return Result.error("ids不能为空");
        }
        JSONObject jsonObject = JSONUtil.parseObj(ids);
        String idsStr = jsonObject.getStr("ids");
        if (StrUtil.isEmpty(idsStr)) {
            return Result.error("ids不能为空");
        }
        applicantsService.nopass(idsStr);
        return Result.OK();
    }
}
